package com.slkj.shilixiaoyuanapp.ui.main;

import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.LazyFragment;
import com.slkj.shilixiaoyuanapp.data.UserRequest;
import com.slkj.shilixiaoyuanapp.entity.UserEntity;
import com.slkj.shilixiaoyuanapp.entity.parent.ChangeRoleEntity;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.ui.user.activity.MyCollectionsActivity;
import com.slkj.shilixiaoyuanapp.ui.user.activity.MyFilesActivity;
import com.slkj.shilixiaoyuanapp.ui.user.activity.PersonalInformationActivity;
import com.slkj.shilixiaoyuanapp.ui.user.activity.SetUpActivity;
import com.slkj.shilixiaoyuanapp.ui.user.activity.MyWagesActivity;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

public class TeacherMyFragment extends LazyFragment {
    @BindView(R.id.img_teacher_head)
    CircleImageView head;
    @BindView(R.id.ll_changeto_parent)
    LinearLayout ll_changeto_parent;
    @BindView(R.id.tv_name)
    TextView tvName;
    UserEntity.User user;
    int schoolId;
    int childId;

    @Override
    protected int getResId() {
        return R.layout.teacher_my_fragment;
    }

    @Override
    protected void onRealLoaded() {
        if (UserRequest.getInstance().getUser().getRoles().size() == 2) {
            if (UserRequest.getInstance().getUser().getRoles().contains(1) && UserRequest.getInstance().getUser().getRoles().contains(2)) {
                ll_changeto_parent.setVisibility(View.VISIBLE);
            }
        }
        user = UserRequest.getInstance().getUser().getUser();
        tvName.setText(user.getUNickName());
        Glide.with(getContext()).load(user.getHeadImg()).error(R.drawable.ic_man).fallback(R.drawable.ic_man).into(head);
    }

    @OnClick({R.id.img_teacher_head, R.id.layout_my_wages, R.id.layout_my_file, R.id.layout_my_collection, R.id.layout_set_up, R.id.ll_changeto_parent})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_teacher_head:
                startActivity(PersonalInformationActivity.class);
                break;
            case R.id.layout_my_wages:
                startActivity(MyWagesActivity.class);
                break;
            case R.id.layout_my_file:
                startActivity(MyFilesActivity.class);
                break;
            case R.id.layout_my_collection:
                startActivity(MyCollectionsActivity.class);
                break;
            case R.id.layout_set_up:
                startActivity(SetUpActivity.class);
                break;
            //切换到家长端
            case R.id.ll_changeto_parent:
                UserRequest.getInstance().getUser().setExtraDatasourse(UserRequest.getInstance().getUser().getDatasource());
                for (int i = 0; i < UserRequest.getInstance().getUser().getStudents().size(); i++) {
                    if (UserRequest.getInstance().getUser().getStudents().get(i).getIsSelect() == 1) {
                        schoolId = UserRequest.getInstance().getUser().getStudents().get(i).getSchoolId();
                        childId = UserRequest.getInstance().getUser().getStudents().get(i).getId();
                    }
                }
                HttpHeper.get().parentService().changeUser(schoolId, childId, UserRequest.getInstance().getUser().getDefaultRole()).compose(getThread())
                        .compose(bindToLifecycle())
                        .subscribe(new CommonCallBack<ChangeRoleEntity>(true, getContext()) {
                            @Override
                            public void onCallBackSuccess(ChangeRoleEntity data) {
                                UserEntity userEntity = UserRequest.getInstance().getUser();
                                userEntity.setDefaultRole(1);
                                userEntity.getUser().setId(data.getUserId());
                                for (int i = 0; i < userEntity.getStudents().size(); i++) {
                                    if (userEntity.getStudents().get(i).getIsSelect() == 1) {
                                        userEntity.setDatasource(userEntity.getStudents().get(i).getDatasource());
                                        break;
                                    }
                                }
                                UserRequest.getInstance().setUser(userEntity);
                                getActivity().finish();
                                Intent intent = new Intent(getContext(), MainActivity.class);
                                startActivity(intent);
                                getActivity().overridePendingTransition(0, 0);
                            }
                        });
                break;
        }
    }
}
